<?php
/**
 *
 */
class GroupsController extends AppController {

	//public $uses = array('Group', 'User');

	public $helpers = array('Html', 'Form');

	public function isAuthorized($user) {
		return true;
	}

	public function index() {
		$user_logged=array(
			'id' => $this->Auth->user('id_user'),
		);
		$this -> set('user_logged', $user_logged);
		$result = $this -> Group -> query('SELECT group_name,id_group FROM groups WHERE groups.id_owner= '.(int)$this->Auth->user('id_user'));
		$this -> set('my_groups', $result);
		$result = $this -> Group -> query('SELECT group_name,groups.id_group,users.name,users.surname,users.email FROM groups,groups_users,users WHERE groups_users.id_user=users.id_user AND groups_users.id_group=groups.id_group AND groups.id_group in( select id_group from groups_users where groups_users.id_user= '.(int)$this->Auth->user('id_user').') order by group_name');
		$this -> set('groups', $result);
	}
	public function add() {
		$data=$this -> request -> data;
		pr($data);
		$save=array(
			'Group' => array(
				'group_name' => $data['Group']['group_name'],
				'id_owner' => $this->Auth->user('id_user'),
			) 
		);
		
		if ($this -> Group -> save($save)) {
			$this -> Session -> setFlash('Your group has been created successfully.');
		} else {
			$this -> Session -> setFlash('Unable to create new Group.');
		}
		$this -> redirect(array('action' => 'index'));
	}

	//Delete the group
	function delete($id_group) {
		// delete the users from the group_users and then delete the group 
		$this->Group->query('delete  from groups_users where id_group='.$id_group);
		if ($this->Group->query('delete  from groups where id_group='.$id_group) ) {
			$this->Session->setFlash('The group with id: ' . $id_group . ' has been deleted.'); 
		    $this->redirect(array('action' => 'index',$id_group));    
		}
	}
	//edit a group
	function edit($id_group) {
		$result = $this -> Group -> query('SELECT groups.group_name,users.id_user,users.name,users.surname,users.email FROM groups,groups_users,users WHERE groups_users.id_user=users.id_user AND groups_users.id_group=groups.id_group and groups.id_group= '.(int)$id_group);
		$this -> set('my_groups', $result);
		$result1 = $this -> Group -> query('SELECT id_group,group_name FROM groups WHERE groups.id_group= '.(int)$id_group);
		$this -> set('my_group_name', $result1);
	}
	//add a user to a group
	public function add_user() {
		$data=$this -> request -> data;
		pr($data);
		$email=$data['Group']['email'];
		$id_group=$data['Group']['id_group'];
		$id_user=$this->Group->query('Select id_user from users where email="'.$email.'"');
		if (count($id_user)==0)
			$this -> Session -> setFlash('Please add a valid email address');
		else
			$this->Group->query('Insert into groups_users (id_group,id_user) Values ('.$id_group.','.$id_user[0]['users']['id_user'].')');
		/*if ($this -> Group -> save($save)) {
			$this -> Session -> setFlash('Your group has been created successfully.');
		} else {
			$this -> Session -> setFlash('Unable to create new Group.');
		}*/
		$this->redirect(array('action' => 'edit', $id_group)); 
	}
	//delete a user from a group
	function delete_user($id_group,$id_user){
		if ($this->Group->query('DELETE FROM groups_users WHERE id_group='.$id_group.' AND id_user='.$id_user) ) {
			$this->Session->setFlash('The user with id: ' . $id_user . ' has been deleted.'); 
		    $this->redirect(array('action' => 'edit',$id_group));    
		}
	}
}
